package com.bootdo.shop.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;

import com.bootdo.shop.dao.provider.OrderMapperProvider;
import com.bootdo.shop.domain.Goods;
import com.bootdo.shop.domain.OrderDetail;
import com.bootdo.shop.domain.OrderInfo;
import com.bootdo.shop.domain.request.OrderInfoReq;
import com.bootdo.shop.domain.response.OrderDetailsRes;
import com.bootdo.shop.domain.response.OrderInfoRes;


/** 
 * @author 作者 : chenxuan
 * @version 创建时间：2016年1月6日 下午4:52:57 
 * 类说明 
 */
public interface OrderMapper {

	@Options(useGeneratedKeys=true)
	@InsertProvider(type=OrderMapperProvider.class,method="createOrderInfo")
	public Integer createOrderInfo(OrderInfo orderInfo);
	
	@Insert("insert into order_detail(order_id,goods_id,goods_name,goods_batch_number,sale_price,real_price,goods_number,product_id) "
			+ " value(#{orderId},#{goodsId},#{goodsName},#{goodsBatchNumber},#{salePrice},#{realPrice},#{goodsNumber},#{productId})")
	public Integer createOrderDetail(OrderDetail orderDetail);
	
	@InsertProvider(type=OrderMapperProvider.class,method="batchCreateOrderDetail")
	public Integer batchCreateOrderDetail(@Param("orderDetails") List<OrderDetail> orderDetails,@Param("orderId") Integer orderId);
	
	
	@Update("update order_info set order_status = #{orderStatus},cancel_time = current_timestamp() where id = #{orderId}")
	public Integer cancelOrderInfo(@Param("orderId") Integer orderId,@Param("orderStatus") Integer orderStatus);
	
	@Update("update order_info set order_status = #{orderStatus},shipping_finish_time = current_timestamp(),"
			+ "shipping_status = #{shippingStatus} where id = #{orderId}")
	public Integer confirmReceipt(@Param("orderId") Integer orderId,@Param("orderStatus") Integer orderStatus,
			@Param("shippingStatus") Integer shippingStatus);
	
	/**
	 * 发货
	 * @param orderId
	 * @param orderStatus
	 * @param shippingStatus
	 * @param shippingChannel
	 * @param shiippingSn
	 * @return
	 */
	@Update("update order_info set order_status = #{orderStatus},"
			+ "shipping_status = #{shippingStatus},shipping_channel = #{shippingChannel},"
			+ "shiipping_sn = #{shiippingSn} where id = #{orderId}")
	public Integer consignment(@Param("orderId") Integer orderId,@Param("orderStatus") Integer orderStatus,
			@Param("shippingStatus") Integer shippingStatus,@Param("shippingChannel") Integer shippingChannel
			,@Param("shiippingSn") String shiippingSn);
	
	/**
	 * 订单支付成功--如果是实体商品
	 * @param orderNo
	 * @param orderStatus
	 * @param payChannel
	 * @param payStatus
	 * @return
	 */
	@Update("update order_info set pay_status = #{payStatus},order_status = 1,"
			+ " pay_time = current_timestamp(),ch_id = #{chId},pay_channel = #{payChannel} where order_no = #{orderNo}" )
	public Integer updateOrderPaySuccessForReal(@Param("orderNo") String orderNo,
			@Param("payChannel") String payChannel,
			@Param("payStatus") Integer payStatus,
			@Param("chId") String chId);
	
	
	/**
	 * 订单支付成功--如果是实体商品
	 * @param orderNo
	 * @param orderStatus
	 * @param payChannel
	 * @param payStatus
	 * @return
	 */
	@Update("update order_info set pay_status = #{payStatus},"
			+ " pay_time = current_timestamp() where id = #{orderId}" )
	public Integer updateOrderPayStatusForPaymentIn(@Param("orderId") Integer orderId,
			@Param("payStatus") Integer payStatus);
	
	/**
	 * 订单支付成功--如果没有实体商品
	 * @param orderNo
	 * @param orderStatus
	 * @param payChannel
	 * @param payStatus
	 * @return
	 */
	@Update("update order_info set pay_channel = #{payChannel},pay_status = #{payStatus},"
			+ " pay_time = current_timestamp(),ch_id = #{chId},order_status = #{orderStatus},pay_channel = #{payChannel}"
			+ " where order_no = #{orderNo}" )
	public Integer updateOrderPaySuccessForService(@Param("orderNo") String orderNo,
			@Param("payChannel") String payChannel,
			@Param("payStatus") Integer payStatus,
			@Param("orderStatus") Integer orderStatus,
			@Param("chId") String chId);
	
	
	@UpdateProvider(type=OrderMapperProvider.class,method="updateOrderAddress")
	public Integer updateOrderAddress(OrderInfoReq orderInfoReq);
	
	@Insert("insert into order_action_log (order_id,order_status,shipping_status,pay_status,action_note,action_user) "
			+ "value"
			+ " (#{orderId},#{orderStatus},#{shippingStatus},#{payStatus},#{actionNote},#{actionUser})")
	public Integer insertOrderActionLog(@Param("orderId") Integer orderId,
			@Param("orderStatus") Integer orderStatus,
			@Param("shippingStatus") Integer shippingStatus,
			@Param("payStatus") Integer payStatus,
			@Param("actionNote") String actionNote,
			@Param("actionUser") String actionUser);
	/**
	 * 更新订单状态为已完成,只用于服务开始介入
	 * @param orderNo
	 * @return
	 */
	@Update("update order_info set order_status = 2, shipping_status = 1 where order_no = #{orderNo}")
	public Integer updateOrderFinishForService(String orderNo);
	
	public String ORDER_INFO_COLUMN = "a.id,a.order_no,a.parent_order_id,a.user_id,a.receiver,a.country,a.pay_type,a.is_comment,"
			+ "a.province,a.city,a.district,a.postcode,a.address,a.tel,a.mobile,a.goods_number,a.goods_weight,a.order_payee,"
			+ "a.shipping_payee,a.total_payee,a.coupon_id,a.coupon_amount,a.is_invoice,a.invoice_id,a.invoice_amount,"
			+ "a.create_time,a.confirm_time,a.pay_channel,a.pay_status,a.pay_time,a.shipping_channel,a.shiipping_sn,"
			+ "a.shipping_status,a.shipping_time,a.shipping_finish_time,a.buyer_memo,a.cancel_time,a.expiration_time,a.order_title,a.ch_id,a.service_mobile,a.fr_amount";
	
	@Select("select * from goods where id = #{goodsId}")
	public Goods getGoodsById(Integer goodsId);
	
	
	@SelectProvider(type=OrderMapperProvider.class,method="userOrderList")
	public List<OrderInfoRes> userOrderLisr(OrderInfoReq orderInfoReq);
	
	@SelectProvider(type=OrderMapperProvider.class,method="userOrderListCount")
	public Integer userOrderLisrCount(OrderInfoReq orderInfoReq);
	
	@Select("select order_id,goods_id,specification_id,goods_name,sale_price,real_price,goods_number,type,"
			+ " thumbnail_image,is_service,service_tag_type,coupon_amount "
			+ " from order_detail where order_id = #{orderId}")
	public List<OrderDetailsRes> getOrderDetailListByOrderId(@Param("orderId") Integer orderId);
	
	/**
	 * 根据ID获取订单信息
	 * @param id
	 * @return
	 */
	@Select("select "+ORDER_INFO_COLUMN+",d.order_status_view order_status,d.order_status_name,b.name shipping_channel_name from order_info a left join order_status_config d "
			+ " on a.order_status = d.order_status and a.pay_status = d.pay_status and a.shipping_status = d.shipping_status and a.pay_type = d.pay_type left join "
			+ " shipping_channels b on a.shipping_channel = b.id  where a.id = #{id}")
	public OrderInfoRes getOrderById(Integer id);
	
	/**
	 * 根据订单ID获取详查 详情
	 * @param orderId
	 * @return
	 */
	@Select("select "+ORDER_INFO_COLUMN+",a.order_status from order_info a where a.id = #{orderId}")
	public OrderInfo getOrderInfoByOrderId(Integer orderId);
	
	/**
	 * 根据ID获取订单信息
	 * @param id
	 * @return
	 */
	@Select("select "+ORDER_INFO_COLUMN+",d.order_status_view order_status from order_info a left join order_status_config d on a.order_status = d.order_status "
			+ " and a.pay_status = d.pay_status and a.shipping_status = d.shipping_status and a.pay_type = d.pay_type where a.order_no = #{orderNo}")
	public OrderInfoRes getOrderByOrderNo(String orderNo);
	
	
	@Select("select * from order_detail where order_id = #{orderId}")
	public List<OrderDetailsRes> getOrderDetailList(Integer orderId);
	
	@Select("select * from order_detail where order_id = #{orderId} and goods_id=#{goodsId}")
	public List<OrderDetailsRes> getOrderDetailByOrderIdAndGoodsId(@Param("orderId") Integer orderId,@Param("goodsId") Integer goodsId);
	
	/**
	 * 统计用户待支付、待收货、待评价订单数
	 * @param userId
	 * @return
	 */
	@Select("select count(case when d.order_status_view = 0 then 1 else null end) non_payment_count,"
			+ "count(case when d.order_status_view = 2 then 1 else null end) shipped_count,"
			+ "count(case when d.order_status_view = 3 and is_comment = 0 then 1 else null end) completed_count"
			+ "  from order_info a left join order_status_config d "
			+ "on a.order_status = d.order_status and a.pay_status = d.pay_status and "
			+ "a.shipping_status = d.shipping_status and a.pay_type = d.pay_type where "
			+ "a.user_id = #{userId} and a.pay_type != 4")
	public OrderInfoRes statisticsOrderCount(Integer userId);
	/**
	 * 已经购买数量
	 * @param userId
	 * @param goodsId
	 * @return
	 */
	@Select("select sum(a.goods_number) goods_number from order_detail a,order_info b where a.order_id = b.id and "
			+ " goods_id = #{goodsId} and user_id = #{userId}  "
			+ " and b.pay_status = 1 and DATE_FORMAT(b.create_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )")
	public Integer fetchGoodsBuyCount(@Param("userId") Long userId,@Param("goodsId") Integer goodsId);
	
	/**
	 * 未支付订单
	 * @param userId
	 * @param goodsId
	 * @return
	 */
	@Select("select sum(a.goods_number) goods_number from order_detail a,order_info b where a.order_id = b.id and "
			+ " goods_id = #{goodsId} and user_id = #{userId}  "
			+ " and  (b.pay_status = 0 and b.expiration_time >=  NOW() ) "
			+ " and DATE_FORMAT(b.create_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )")
	public Integer fetchUnpayGoodsBuyCount(@Param("userId") Long userId,@Param("goodsId") Integer goodsId);
	
	@Select("select count(1) cnt from store_delivery_list where order_id = #{orderId}")
	public Integer checkStoreDeliveryList(Integer orderId);
	/**
	 * 根据订单号列表查找订单列表
	 * @param orderNoList
	 * @return
	 */
	@SelectProvider(type=OrderMapperProvider.class,method="getOrderListForOrderNoList")
	public List<OrderInfo> getOrderListForOrderNoList(@Param("orderNoList") List<String> orderNoList);
	
	/**查询优惠券是否已使用**/
	@Select("select count(1) from order_info where order_status > 0 and coupon_id = #{couponId}")
	public Integer getCouponIsUsed(Integer couponId) ;
	/**
	 * 根据第三方订单号查询系统中是否有已经存在
	 * @param thirdpartyOrderNo
	 * @return
	 */
	@Select("select * from order_info where thirdparty_order_no = #{thirdpartyOrderNo} and order_status > 0 limit 1")
	public OrderInfoRes getOrderByThirdpartyOrderNo(String thirdpartyOrderNo);
	
}
